<?php
// +----------------------------------------------------------------------
// | 杭州十禾信息科技有限公司
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2015 http://www.shelpe.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: LeePuman <ysyzlsm@163.com>
// +----------------------------------------------------------------------

class AdminModel extends NewzanModel{

	/**
     *
	 * 获取系统中维护客户里诶包
	 * @param string $keyword 搜索用户的关键字，留空的时候默认是全部用户
	 * @param number $page 当前页数
	 * @param number $limit 每页多少用户
	 * @param string $sort 排序字段，可以是统计报表中的常见所有字段，外加上balance字段
	 * @param string $desc 倒叙或者顺序排序，'desc' , 'asc'
	 * @return mixed 返回用户的列表
	 */
	public function getCustomers($page =1, $limit = 20, $sort='purchased_time' , $desc = 'desc' , $filters = array() , $tag = ''){
        $whr = array();
        if( !is_array($filters) ) $filters = explode(',' , $filters);
        if( !empty( $filters ) ){
            $filters_map = array(
                'paused' => ' ( 1.0 * paused_adgroup_num /total_adgroup_num)  > 0.5 ',
                'unused' => ' auto_adgroup_num = 0 ',
                'cost' => ' cost > 0 and sales = 0',
                'noclick' => ' auto_adgroup_num > 0 and click = 0 ' , 
            );
            if( isset( $filters['keyword'] ) ){
                $whr[] =  'nick LIKE "%' . $filters['keyword'] . '%" ' ;
            }else{
                foreach($filters as $f){
                    if($f =='all') continue;
                    $whr[] = $filters_map[$f]; 
                }
            }
        }

        if ( $tag ) $whr[] = ' tags LIKE "%' .$tag. '%" ';
        if( $whr ) $whr = implode(' AND ' , $whr );
        $mdl = M('customers');
        $customers = $mdl->where( $whr )->order( " $sort $desc " )->limit( $limit )->page( $page )->select(); 
        return $customers;
    }
}

